<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------

namespace Addons\EditorForAdmin\Controller;
use Home\Controller\AddonsController;
use Think\Upload;
use Think\Upload\Driver\Qiniu\QiniuStorage;

class UploadController extends AddonsController{

	public $uploader = null;

	/* 上传图片 */
	public function upload(){
		session('upload_error', null);
		/* 上传配置 */
		$setting = C('EDITOR_UPLOAD');
		
		/* 调用文件上传组件上传文件 */
		$this->uploader = new Upload($setting, 'Local');
		$info   = $this->uploader->upload($_FILES);
		if($info){
			$url = C('EDITOR_UPLOAD.rootPath').$info['imgFile']['savepath'].$info['imgFile']['savename'];
			$url = str_replace('./', '/', $url);
			$info['fullpath'] = __ROOT__.$url;
		}
		session('upload_error', $this->uploader->getError());
		return $info;
	}

	//keditor编辑器上传图片处理 
	public function ke_upimg(){
		/* 返回标准数据 */
		$return  = array('error' => 0, 'info' => '上传成功', 'data' => '');
		$img = $this->upload();
		/* 记录附件信息 */
		if($img){
			$return['url'] = $img['fullpath'];
			unset($return['info'], $return['data']);
		} else {
			$return['error'] = 1;
			$return['message']   = session('upload_error');
		} 
		/* 返回JSON数据 */
		exit(json_encode($return));
	}

	//ueditor编辑器上传图片处理 改用七牛上传 
	public function ue_upimg(){  
		$return  = array('status' => 1, 'info' => '上传成功', 'data' => '');
		/* 调用文件上传组件上传文件 */
		$File = D('File');
		$file_driver = C('DOWNLOAD_UPLOAD_DRIVER');
		//默认使用七牛上传组件  
		$upInfo = $File->upload(
			$_FILES,
			C('DOWNLOAD_UPLOAD'),
			C('DOWNLOAD_UPLOAD_DRIVER'),
			C("DOWNLOAD_{$file_driver}_CONFIG")
		); 
		$info = array_shift( $upInfo ) ;
		//怕七牛替换不完整 
		$up_qiniu_config = C("DOWNLOAD_{$file_driver}_CONFIG") ;
		$info['url'] 	 = str_replace("http:///", "http://".$up_qiniu_config['domain']."/", $info['url']) ;
        /* 记录上传后图片信息 */
        if($info){       
            $return['data']  = think_encrypt(json_encode($info['imgFile']));
            $return['url']   = $info['url'] ;
            $return['title'] = $info['name'] ; 
            $return['original'] = $info['name'] ;  
            $return['state']  = 'SUCCESS' ;
        } else {
            $return['status'] = 0;
            $return['info']   = $File->getError();
        }
		/* 返回JSON数据 */
		$this->ajaxReturn($return);
	}

	//ueditor编辑器上传视频处理 改用七牛上传 
	public function ue_upvideo(){  
		$return  = array('status' => 1, 'info' => '上传成功', 'data' => '');
		/* 调用文件上传组件上传文件 */
		$File = D('File'); 
		$file_driver = C('DOWNLOAD_UPLOAD_DRIVER');
		$video_upload = C('VIDEO_UPLOAD') ;   
		//默认使用七牛上传组件  
		$upInfo = $File->upload(
			$_FILES,
			$video_upload ,
			C('DOWNLOAD_UPLOAD_DRIVER'),
			C("UPLOAD_QINIU_CONFIG_VIDEO")
		);  
		$info = array_shift( $upInfo ) ;
		//怕七牛替换不完整 
		$up_qiniu_config = C("UPLOAD_QINIU_CONFIG_VIDEO") ; 
        /* 记录上传后视频信息 */
        if($info){       
        	$info['url'] 	 = str_replace("http:///", "http://".$up_qiniu_config['domain']."/", $info['url']) ;
            $return['data']  = think_encrypt(json_encode($info['imgFile']));
            $return['url']   = $info['url'] ;
            $return['title'] = $info['name'] ; 
            $return['original'] = $info['name'] ;  
            $return['state']  = 'SUCCESS' ;
        } else {
            $return['status'] = 0;
            $return['info']   = $File->getError();
        }
        if (  $return['status'] != 0 ) {
        	// 这里请求视频截图持久化 + 回调处理URL 
	        $config = C('UPLOAD_QINIU_CONFIG_VIDEO') ;
	        $qiniu  = new QiniuStorage($config);
	        //添加一个持续化任务 
	        $file   = array_pop( explode('/', $return['url']) );;  
	        //生成文件名   
	        $fops       = "vframe/jpg/offset/1/w/480/h/360" ; //视频截图的缩略图  
	       	$_SERVER['HTTP_HOST'] = 'www.zhihuiluanchuan.com' ; 
	        $notifyURL  = 'http://'.$_SERVER['HTTP_HOST'].U('Home/Qiniucall/video_notify') ;
	        //创建持久化任务 生成缩略图 成功返回任务队列ID  
	        $ar         = $qiniu->pfop( $file , $fops , $notifyURL ) ; 
	        M('video_thumb')->add(array('list_no'=>$ar,'input_key'=>$file ) ) ;
        } 
		/* 返回JSON数据 */
		$this->ajaxReturn($return);
	}

}
